<?php

require('fpdf/fpdf.php');

class PDF extends FPDF {

	/**
	 * Default font size used in this Report
	 *
	 * @var integer
	 */
	private $fontSize = 10;
	/**
	 * Top margin for A4 Paper
	 */
	const A4_TOP_MARGIN = 20;

	/**
	 * Left margin for A4 Paper
	 */
	const A4_LEFT_MARGIN = 17;

	/**
	 * DO NOT MODIFY THIS!
	 * A4 paper width in mm
	 */
	const A4_WIDTH = 210;

	/**
	 * DO NOT MODIFY THIS!
	 * A4 paper height in mm
	 */
	const A4_HEIGHT = 297;

	/**
	 * {@inherit}
	 */
	public function __construct($orientation="P", $unit="mm", $format="A4") {
		parent::FPDF($orientation, $unit, $format);
		$this->SetMargins(self::A4_LEFT_MARGIN, self::A4_TOP_MARGIN);
	}

	/**
	 * Returns the current usable area (paper width - margin)
	 *
	 * @return integer
	 */
	public function getViewportWidth() {
		if ("P" == $this->CurOrientation)
			return self::A4_WIDTH - self::A4_LEFT_MARGIN * 2;
		return self::A4_HEIGHT - self::A4_TOP_MARGIN * 2;
	}

	/**
	 * DO NOT USE
	 *
	 * Prints page number
	 */
	public function Header() {
		$this->SetFont('Arial', 'B', 10);
		$text = __("Page") . " " . $this->PageNo();
		$width = $this->GetStringWidth($text);
		$this->Cell($this->getViewportWidth() - $width);
		$this->Cell($width, 7, $text, 0, 0, 'C');
		$this->Ln();
	}

	/**
	 * {@inherit}
	 *
	 * Overhidden method, provides wrapping text cells
	 * Code from http://www.fpdf.de/downloads/addons/32/
	 */
	public function Cell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=0, $link='') {
		//Output a cell
		$k = $this->k;
		if ($this->y + $h > $this->PageBreakTrigger and !$this->InFooter and $this->AcceptPageBreak()) {
			$x = $this->x;
			$ws = $this->ws;
			if ($ws > 0) {
				$this->ws = 0;
				$this->_out('0 Tw');
			}
			$this->AddPage($this->CurOrientation);
			$this->x = $x;
			if ($ws > 0) {
				$this->ws = $ws;
				$this->_out(sprintf('%.3f Tw', $ws * $k));
			}
		}
		if ($w == 0)
			$w = $this->w - $this->rMargin - $this->x;
		$s = '';
		if ($fill == 1 || $border > 0) {
			if ($fill == 1)
				$op = ($border > 0) ? 'B' : 'f';
			else
				$op = 'S';
			if ($border > 1) {
				$s = sprintf(" q %.2f w %.2f %.2f %.2f %.2f re %s Q ", $border, $this->x * $k, ($this->h - $this->y) * $k, $w * $k, -$h * $k, $op);
			}
			else
				$s = sprintf("%.2f %.2f %.2f %.2f re %s ", $this->x * $k, ($this->h - $this->y) * $k, $w * $k, -$h * $k, $op);
		}
		if (is_string($border)) {
			$x = $this->x;
			$y = $this->y;
			if (is_int(strpos($border, 'L')))
				$s.=sprintf('%.2f %.2f m %.2f %.2f l S ', $x * $k, ($this->h - $y) * $k, $x * $k, ($this->h - ($y + $h)) * $k);
			else if (is_int(strpos($border, 'l')))
				$s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ', $x * $k, ($this->h - $y) * $k, $x * $k, ($this->h - ($y + $h)) * $k);
			if (is_int(strpos($border, 'T')))
				$s.=sprintf('%.2f %.2f m %.2f %.2f l S ', $x * $k, ($this->h - $y) * $k, ($x + $w) * $k, ($this->h - $y) * $k);
			else if (is_int(strpos($border, 't')))
				$s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ', $x * $k, ($this->h - $y) * $k, ($x + $w) * $k, ($this->h - $y) * $k);
			if (is_int(strpos($border, 'R')))
				$s.=sprintf('%.2f %.2f m %.2f %.2f l S ', ($x + $w) * $k, ($this->h - $y) * $k, ($x + $w) * $k, ($this->h - ($y + $h)) * $k);
			else if (is_int(strpos($border, 'r')))
				$s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ', ($x + $w) * $k, ($this->h - $y) * $k, ($x + $w) * $k, ($this->h - ($y + $h)) * $k);
			if (is_int(strpos($border, 'B')))
				$s.=sprintf('%.2f %.2f m %.2f %.2f l S ', $x * $k, ($this->h - ($y + $h)) * $k, ($x + $w) * $k, ($this->h - ($y + $h)) * $k);
			else if (is_int(strpos($border, 'b')))
				$s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ', $x * $k, ($this->h - ($y + $h)) * $k, ($x + $w) * $k, ($this->h - ($y + $h)) * $k);
		}
		if (trim($txt) != '') {
			$cr = substr_count($txt, "\n");
			if ($cr > 0) { // Multi line
				$txts = explode("\n", $txt);
				$lines = count($txts);
				//$dy=($h-2*$this->cMargin)/$lines;
				for ($l = 0; $l < $lines; $l++) {
					$txt = $txts[$l];
					$w_txt = $this->GetStringWidth($txt);
					if ($align == 'R')
						$dx = $w - $w_txt - $this->cMargin;
					elseif ($align == 'C')
						$dx = ($w - $w_txt) / 2;
					else
						$dx = $this->cMargin;
					$txt = str_replace(')', '\\)', str_replace('(', '\\(', str_replace('\\', '\\\\', $txt)));
					if ($this->ColorFlag)
						$s.='q ' . $this->TextColor . ' ';
					$s.=sprintf('BT %.2f %.2f Td (%s) Tj ET ', ($this->x + $dx) * $k, ($this->h - ($this->y + .5 * $h + (.7 + $l - $lines / 2) * $this->FontSize)) * $k, $txt);
					if ($this->underline)
						$s.=' ' . $this->_dounderline($this->x + $dx, $this->y + .5 * $h + .3 * $this->FontSize, $txt);
					if ($this->ColorFlag)
						$s.='Q ';
					if ($link)
						$this->Link($this->x + $dx, $this->y + .5 * $h - .5 * $this->FontSize, $w_txt, $this->FontSize, $link);
				}
			} else { // Single line
				$w_txt = $this->GetStringWidth($txt);
				$Tz = 100;
				if ($w_txt > $w - 2 * $this->cMargin) { // Need compression
					$Tz = ($w - 2 * $this->cMargin) / $w_txt * 100;
					$w_txt = $w - 2 * $this->cMargin;
				}
				if ($align == 'R')
					$dx = $w - $w_txt - $this->cMargin;
				elseif ($align == 'C')
					$dx = ($w - $w_txt) / 2;
				else
					$dx = $this->cMargin;
				$txt = str_replace(')', '\\)', str_replace('(', '\\(', str_replace('\\', '\\\\', $txt)));
				if ($this->ColorFlag)
					$s.='q ' . $this->TextColor . ' ';
				$s.=sprintf('q BT %.2f %.2f Td %.2f Tz (%s) Tj ET Q ', ($this->x + $dx) * $k, ($this->h - ($this->y + .5 * $h + .3 * $this->FontSize)) * $k, $Tz, $txt);
				if ($this->underline)
					$s.=' ' . $this->_dounderline($this->x + $dx, $this->y + .5 * $h + .3 * $this->FontSize, $txt);
				if ($this->ColorFlag)
					$s.='Q ';
				if ($link)
					$this->Link($this->x + $dx, $this->y + .5 * $h - .5 * $this->FontSize, $w_txt, $this->FontSize, $link);
			}
		}
		if ($s)
			$this->_out($s);
		$this->lasth = $h;
		if ($ln > 0) {
			//Go to next line
			$this->y+=$h;
			if ($ln == 1) {
				$this->x = $this->lMargin;
			}
		} else {
			$this->x+=$w;
		}
	}

	/**
	 * Given a text, calculates the total number of lines, and wraps the text
	 *
	 * Code from http://www.fpdf.de/downloads/addons/49/
	 *
	 * @param string	$text
	 * @param integer	$maxwidth
	 * @return integer
	 */
	public function WordWrap(&$text, $maxwidth) {
		$text = trim($text);
		if ($text === '')
			return 0;
		$space = $this->GetStringWidth(' ');
		$lines = explode("\n", $text);
		$text = '';
		$count = 0;
		foreach ($lines as $line) {
			$words = preg_split('/ +/', $line);
			$width = 0;
			foreach ($words as $word) {
				$wordwidth = $this->GetStringWidth($word);
				if ($width + $wordwidth <= $maxwidth) {
					$width += $wordwidth + $space;
					$text .= $word . ' ';
				} else {
					$width = $wordwidth + $space;
					$text = rtrim($text) . "\n" . $word . ' ';
					$count++;
				}
			}
			$text = rtrim($text) . "\n";
			$count++;
		}
		$text = rtrim($text);
		return $count;
	}

}

// End PDF